package com.example.view.module.scroll.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.common.base.AbsFragment;
import com.example.view.R;
import com.example.view.databinding.FragDesignScrollPageBinding;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class PageFragment extends AbsFragment<FragDesignScrollPageBinding> {

    private RvAdapter mAdapter;
    private View loadView;
    private int num = 0;
    private List<String> data;

    public static PageFragment getInstance() {
        return new PageFragment();
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected void initView(View view) {
        data = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            data.add("item num==" + i);
        }
        num = data.size() - 1;
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new RvAdapter(data);
        mBinding.rv.setAdapter(mAdapter);
        loadView = LayoutInflater.from(mContext).inflate(R.layout.view_loading, null);
        mAdapter.addFooterView(loadView);
    }

    @Override
    protected void initListener() {
        mBinding.rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (isSlideToBottom(recyclerView)) {
                    Toast.makeText(mContext, "slide to button", Toast.LENGTH_SHORT).show();
                    if (num > 100) {
                        TextView tv_name = loadView.findViewById(R.id.tv_name);
                        tv_name.setText("没有更多数据了");
                        return;
                    }
                    List<String> newData = new ArrayList<>();
                    for (int i = num; i < num+10; i++) {
                        newData.add("item num==" + i);
                    }
                    data.addAll(data.size(), newData);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyItemRangeChanged(num, newData.size());
                            num = data.size();
                        }
                    }, 2000);

                }
            }
        });

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Toast.makeText(mContext, "item==" + data.get(position), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_design_scroll_page;
    }

    @Override
    protected void onRefresh() {

    }

    /**
     * 是否滑动到底部
     *
     * @param recyclerView
     * @return
     */
    private boolean isSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }

    class RvAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public RvAdapter(@Nullable List<String> data) {
            super(R.layout.item_text, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder viewHolder, String s) {
            viewHolder.setText(R.id.tv_name, s);
        }
    }


}
